package com.htgd.gardenexpo.controller;


import com.htgd.gardenexpo.dto.GeConFreeSportsDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConFreeSports;
import com.htgd.gardenexpo.entity.GeConHealthy;
import com.htgd.gardenexpo.service.IGeConFreeSportsService;
import com.htgd.gardenexpo.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.htgd.gardenexpo.utils.TokenUtil.getUsernameFromToken;

/**
 * <p>
 * 自由跑 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/api/freeSports")
@Api(tags = "自由跑")
@Slf4j
public class GeConFreeSportsController {

    @Autowired
    private IGeConFreeSportsService geConFreeSportsService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add(@RequestBody GeConFreeSportsDTO geConFreeSportsDTO, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("自由跑-添加：{}，{}",geConFreeSportsDTO,openId);
        return geConFreeSportsService.add(geConFreeSportsDTO,openId);
    }

    @GetMapping(value = "/getMyRecord")
    @ApiOperation("获取我的运动记录")
    public CommonResult getMyRecord(@RequestParam(name = "pageNum",required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize",required = false, defaultValue = "20") Integer pageSize,
                                    HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("自由跑-获取我的运动记录：{},{},{}",openId,pageNum,pageSize);
        return geConFreeSportsService.getMyRecord(openId,pageNum,pageSize);
    }


    @GetMapping(value = "/getTaskDetails")
    @ApiOperation("获取任务详情")
    public CommonResult getTaskDetails(@RequestParam(value = "taskId") String taskId) {
        log.info("自由跑-获取任务详情：{}",taskId);
        return geConFreeSportsService.getTaskDetails(taskId);
    }

}
